package com.jiuwan.gamepass.service.impl;

import com.jiuwan.gamepass.dao.AppDao;
import com.jiuwan.gamepass.dao.FeedbackDao;
import com.jiuwan.gamepass.dao.IconAppDao;
import com.jiuwan.gamepass.dto.appJson.FeedBackJson;
import com.jiuwan.gamepass.dto.appJson.IconJson;
import com.jiuwan.gamepass.pojo.Feedback;
import com.jiuwan.gamepass.pojo.IconApp;
import com.jiuwan.gamepass.service.IconService;
import com.jiuwan.gamepass.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @version 1.0
 * 功能目的描述 ：用于 icon图片上传   ========>>>>>>>>>>>
 * @author： pang-yun
 * @date： 2021-06-08 09:32
 */
@Slf4j
@Service
public class IconServiceImpl implements IconService {
    private static final String SIZE = "1024*1024";


    @Autowired
    private AppDao appDao;

    @Autowired
    private IconJson iconJson;

    @Autowired
    private IconAppDao iconAppDao;

    @Autowired
    private Feedback feedback;

    @Autowired
    private FeedbackDao feedbackDao;

    @Autowired
    private FeedBackJson feedBackJson;

    @Override
    public IconJson uploadIcon(MultipartFile[] files, String appID) {


        if (files == null || files.length == 0) {
            iconJson.setCode(0);
            iconJson.setMsg("请选中要上传的文件");
            return iconJson;
        }

        iconJson.setCode(1);
        iconJson.setMsg("上传成功");
        HashMap<String, String> map = new HashMap<>();
        ArrayList<IconApp> iconList = new ArrayList<>();
        int i1 = iconAppDao.deleteByAppId(appID);
        log.info("删除原icon数据库{}条信息", i1);

        for (int i = 0; i < files.length; i++) {
            MultipartFile file = files[i];

            //限制获取图片的大小
            String s = ImageSizeUtil.imageSize(file);
            System.out.println(s);
            if (s.equals(SIZE) == false) {
                iconJson.setCode(0);
                iconJson.setMsg("上传失败,icon尺寸出错，必须是1024*1024");
                return iconJson;
            }

            //调用自己写的上传文件工具类
            String imagePath = UploadUtils.upload(file);
            if (imagePath != null) {
                //将图片信息方法iconApp中
                IconApp iconApp = new IconApp();
                String uuid = UUIDUtil.createUUID();
                iconApp.setId(uuid);
                iconApp.setAppId(appID);
                iconApp.setIcon(imagePath);
                Date date = new Date();
                iconApp.setUpdateTime(date);
                iconAppDao.insert(iconApp);

                map.put("uuid" + "-" + i, uuid);
                map.put("src" + "-" + i, imagePath);

                iconList.add(iconApp);
            } else {
                iconJson.setCode(0);
                iconJson.setMsg("fail");
                log.info("icon上传阶段失败");
            }
        }
        iconJson.setData(map);
        iconJson.setIcons(iconList);
        return iconJson;


    }

    @Override
    public List<IconApp> selectIcon(String appID) {
        List<IconApp> iconApps = iconAppDao.selectByAppID(appID);
        return iconApps;
    }

    @Override
    public IconJson deleteIcon(String appID) {
        List<IconApp> iconApps = iconAppDao.selectByAppID(appID);
        log.info("现将 {}储存的图片删除 ", iconApps);
        log.info("==============删除位置后期需要自己更改");

        for (IconApp iconApp : iconApps) {
            String icon = iconApp.getIcon();
            String fileName = "G:\\MyProgran\\";
            String string = icon.split("8080/")[1].toString();
            fileName = fileName + string;

            boolean delete = DeleteFileUtil.delete(fileName);

        }

        int i = iconAppDao.deleteByAppId(appID);
        log.info("删除了{}条数据", i);
        iconJson.setCode(MyCode.SUCCESS_CODE);
        iconJson.setMsg(MyCode.SUCCESS_MSG + "-----list中为被删除的所有记录");
        iconJson.setIcons(iconApps);
        return iconJson;

    }


}
